package com.googlecode.studentrecords;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;

/**Interacts with SRDatabase to display GUI*/
public class SRView extends JFrame{

	JLabel userIDLabel, passwordLabel, instructions, 
				studentRecord, nameLabel, addressLabel, 
											eContactLabel;
	JTextField userIDField, passwordField, idField, 
				nameField, addressField, eContactField;
	JButton loginButton, submitButton, cancelButton;
	String userid, name, address, eContact, password,
		newUserID, newName, newAddress, newEContact;
	String[] userData;
	JFrame frame;
	JPanel panel, informationPane;
	JMenuBar menuBar;
	JMenu fileMenu, updateMenu;
	JMenuItem logout, quit, updateInfo, changePassword;
	
	private SRDatabase model;

	/**Creates GUI view using data from 
	 * SRDatabase
	 * 
	 * @param newModel
	 */
	public SRView(SRDatabase newModel){

		model = newModel;
		
		frame = new JFrame("Student Records");
		panel = new JPanel();
	
		panel.setLayout(new BoxLayout(panel, 
								BoxLayout.LINE_AXIS));
		
		JPanel leftHalf = new JPanel();
		JPanel rightHalf = new JPanel();

		leftHalf.setLayout(new GridBagLayout());
		leftHalf.add(createLoginPane());
		rightHalf.add(createInstructions());
		//leftHalf.add(createButton());
		
		panel.add(leftHalf);
		panel.add(rightHalf);
		
		frame.add(panel);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	/**Creates Login page layout
	 * 
	 * @return login page layout
	 */
	public JComponent createLoginPane(){
		
		userIDField = new JTextField();
		userIDField.setColumns(10);
		
		userIDLabel = new JLabel("User ID: ");
		
		passwordField = new JPasswordField();
		passwordField.setColumns(10);
		
		passwordLabel = new JLabel("Password: ");
		
		loginButton = new JButton("Login!");
		
		JPanel loginPane = new JPanel();
		loginPane.setLayout(new GridBagLayout());
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(10, 15, 10, 15);
		
		gbc.gridx = 0;
		gbc.gridy = 0;
		loginPane.add(userIDLabel, gbc);
		gbc.gridx = 2;
		gbc.gridy = 0;
		loginPane.add(userIDField, gbc);
		gbc.gridx = 0;
		gbc.gridy = 2;
		loginPane.add(passwordLabel, gbc);
		gbc.gridx = 2;
		gbc.gridy = 2;
		loginPane.add(passwordField, gbc);
		
		gbc.gridx = 2;
		gbc.gridy = 3;
		loginPane.add(loginButton, gbc);
		
		return loginPane;
	}
	
	/**Creates instructions pane layout
	 * 
	 * @return instruction pane layout
	 */
	public JComponent createInstructions(){
		
		JPanel instructionsPane = new JPanel();

		
		JTextArea instructionsArea = new JTextArea(5,20);
		instructionsArea.setEditable(false);
		instructionsArea.setLineWrap(true);
		instructionsArea.setWrapStyleWord(true);
		instructionsArea.setBackground(
					UIManager.getColor("Panel.background"));
		
		String instructions = ("Fictional University "
				+ "Student Records "
				+ "\n-------------------------------"
				+ "--------------------\n"
				+ "Welcome! Please sign in at the left "
				+ "to get started.");
		instructionsArea.setText(instructions);
		
		instructionsPane.add(instructionsArea);
		
		return instructionsPane;
	}
	
	/**Checks if user input password matches
	 * the same one in database
	 * 
	 * @return True if passwords are equal
	 */
	public boolean passwordCheck(){
		String[] student = new String[5];
		
		userid = userIDField.getText();
		String password = passwordField.getText();
		int studentIndex;
		
		studentIndex = model.findStudent(userid);
		if ((studentIndex == -1) || 
							(userid.length() != 6))
		{
			return false;
		}
		
		student = model.getStudent(userid);
		String storedPassword = student[4];
		
		if (password.equals(storedPassword)){
			return true;
		}
		else {
			return false;
		}
	}
	
	/**Retrieves listeners for login page
	 * 
	 * @param cal 	Accept ActionListener object
	 */
	 void addLoginListener(ActionListener cal) {
	        loginButton.addActionListener(cal);
	    }
	 
	 /**Retrieves listeners for menu
	 * 
	 * @param menu 	Accept ActionListener object
	 */
	 void addMenuListener(ActionListener menu) {
		 logout.addActionListener(menu);
		 quit.addActionListener(menu);
		 updateInfo.addActionListener(menu);
	 }
	 
	 /**addUpdateListener retrieves listeners for
	  * update page
	  * 
	  * @param update Accept ActionListener object
	  */
	 void addUpdateListener(ActionListener update) {
		 userIDField.addActionListener(update);
		 nameField.addActionListener(update);
		 addressField.addActionListener(update);
		 eContactField.addActionListener(update);
		 
		 submitButton.addActionListener(update);
		 cancelButton.addActionListener(update);
	 }
	 
	 /**Creates Information page layout
	  * 
	  * @return 	Returns Information page layout
	  */
	 JComponent createInformationPane() {	 
		 
		informationPane = new JPanel();
		informationPane.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(10, 15, 10, 15);

		String studentRecord;
		studentRecord = model.getFormattedRecord(userid);
		
		
		
		JTextArea studentRecordArea = new JTextArea(5,20);
		studentRecordArea.setEditable(false);
		studentRecordArea.setLineWrap(true);
		studentRecordArea.setWrapStyleWord(true);
		studentRecordArea.setBackground(
					UIManager.getColor("Panel.background"));
		
		gbc.gridx = 0;
		gbc.gridy = 0;
		informationPane.add(studentRecordArea, gbc);
		studentRecordArea.setText(studentRecord);
		
		return informationPane; 
	 }
	 
	 /**Shows pane in the current frame
	  * 
	  * @param pane 	Accepts pane to be dsplayed
	  */
	 void showPane(JComponent pane){
		 
		 panel.removeAll();
		 panel.add(pane);
		 frame.add(panel);
		 frame.pack();
		 
	 }
	 
	 /**Creates menu and displays it
	  * 
	  */
	 void createMenu(){
		 menuBar = new JMenuBar();
		 fileMenu = new JMenu("File");
		 updateMenu = new JMenu("Update");
		 
		 menuBar.add(fileMenu);
		 menuBar.add(updateMenu);
		 
		 logout = new JMenuItem("Logout");
		 quit = new JMenuItem("Quit");
		 updateInfo = new JMenuItem("Update Info...");
		 changePassword = new JMenuItem("Change Password..");
		 
		 fileMenu.add(logout);
		 fileMenu.add(quit);
		 updateMenu.add(updateInfo);
		 updateMenu.add(changePassword);
		 
		 frame.setJMenuBar(menuBar);
		 frame.pack();
	 }
	 
	 /**Creates Update page layout
	  * 
	  * @return 	Returns Update page layout
	  */
	 JComponent createUpdatePane() {
		
		userData = model.getStudent(userid);
		name = userData[1];
		address = userData[2];
		eContact = userData[3];
		
		JPanel updatePane = new JPanel();
		userIDField = new JTextField(userid);
		userIDField.setColumns(10);
		userIDLabel = new JLabel("User ID: ");
		
		nameField = new JTextField(name);
		nameField.setColumns(10);
		nameLabel = new JLabel("Name: ");
		
		addressField = new JTextField(address);
		addressField.setColumns(10);
		addressLabel = new JLabel("Address: ");
		
		eContactField = new JTextField(eContact);
		eContactField.setColumns(10);
		eContactLabel = new JLabel("Emergency Contact: ");
		
		submitButton = new JButton("Submit");
		cancelButton = new JButton("Cancel");
		
		updatePane.setLayout(new GridBagLayout());
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(10, 15, 10, 15);
		
		gbc.gridx = 0;
		gbc.gridy = 0;
		updatePane.add(userIDLabel, gbc);
		gbc.gridx = 2;
		gbc.gridy = 0;
		updatePane.add(userIDField, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 2;
		updatePane.add(nameLabel, gbc);
		gbc.gridx = 2;
		gbc.gridy = 2;
		updatePane.add(nameField, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 3;
		updatePane.add(addressLabel, gbc);
		gbc.gridx = 2;
		gbc.gridy = 3;
		updatePane.add(addressField, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 4;
		updatePane.add(eContactLabel, gbc);
		gbc.gridx = 2;
		gbc.gridy = 4;
		updatePane.add(eContactField, gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 5;
		updatePane.add(submitButton, gbc);
		
		gbc.gridx = 2;
		gbc.gridy = 5;
		updatePane.add(cancelButton,gbc);
		
		return updatePane;
	 }
	 
	 /**Updates student records based on
	  * new values
	  */
	 void updateFields(){
		
		newUserID = userIDField.getText();
		newName = nameField.getText();
		newAddress = addressField.getText();
		newEContact = eContactField.getText();
		
		if(userid != newUserID)
		{
			model.updateID(userid, newUserID);
			userid = newUserID;
		}
		
		if(name != newName)
		{
			model.updateName(userid, newName);
		}
		
		if(address != newAddress)
		{
			model.updateAddress(userid, newAddress);
		}
		
		if(eContact != newEContact)
		{
			model.updateEmergencyContact(userid, newEContact);
		}

		System.out.println(model.getFormattedRecord(userid));
		 
	 }
}

